package com.missilike.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.missilike.repository.entity.MilPhoto;
import com.missilike.repository.mapper.MilPhotoMapper;
import com.missilike.biz.IMilPhotoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author pengpengli
 * @since 2018-12-19
 */
@Service
@Slf4j
public class MilPhotoServiceImpl extends ServiceImpl<MilPhotoMapper, MilPhoto> implements IMilPhotoService {

    @Override
    public Long upsertPhotoId(Long photoId, String filePath) {
        if (StringUtils.isEmpty(filePath)) {
            return 0L;
        }
        MilPhoto photo = this.getById(photoId);
        if (photo == null) {
            photo = new MilPhoto();
        }
        photo.setPhotoUrl(filePath);
        photo.setPhotoType(0);
        this.saveOrUpdate(photo);
        return photo.getId();
    }

    @Override
    public String getPhotoUrl(Long photoId) {
        MilPhoto photo = this.getById(photoId);
        if (photo != null) {
            return photo.getPhotoUrl();
        }
        return "";
    }

    @Override
    public Long insertPhotoUrl(String filePath) {
        MilPhoto photo = new MilPhoto();
        photo.setPhotoUrl(filePath);
        photo.setPhotoType(0);
        this.baseMapper.insert(photo);
        return photo.getId();
    }

    @Override
    public List<MilPhoto> getPhotoUrls(Long[] ids) {
        return this.list(new QueryWrapper<MilPhoto>().lambda().in(MilPhoto::getId, ids));
    }
}
